package dao;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import daoListener.DAOListeneter;

/**
 * @author usuario
 *
 * @param <T>
 */
public abstract class GenericoDAO<T extends Object> {

	protected FileReader arq = null;
	protected BufferedReader buffer = null;
	protected PrintWriter prop = null;
	private String pathName;
	private List<DAOListeneter> listeners;

	/**
	 * @param pathName
	 */
	public GenericoDAO(String pathName) {
		this.pathName = pathName;
		try {
			arq = new FileReader(pathName);
			buffer = new BufferedReader(arq);
			prop = new PrintWriter(pathName);
			listeners = new ArrayList<DAOListeneter>();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @return O nome do arquivo que grarda os dados
	 */
	public String getPathName() {
		return pathName;
	}

	/**
	 * @param pathName
	 */
	public void setPathName(String pathName) {
		this.pathName = pathName;
	}

	/**
	 * @return A Lista de Objeto
	 * @throws Exception
	 */
	public abstract List<T> carregar() throws Exception;

	/**
	 * @param objets
	 * @throws Exception
	 */
	public abstract void salvar(List<T> objets) throws Exception;

	/**
	 * @param listener
	 */
	public void addListener(DAOListeneter listener){
		listeners.add(listener);
	}
	/**
	 * @param message
	 */
	public void carregouNotifyAll(String message){
		for(DAOListeneter listener:listeners){
			listener.carregou(message);
		}
	}
	
	/**
	 * @param message
	 */
	public void salvouNotifyAll(String message){
		for(DAOListeneter listener:listeners){
			listener.salvou(message);
		}
	}
}
